<template>
    <!-- 核销 -->
    <view class="helper-verification" :class="{ 'helper-verification-light': changeType == 1 }">
        <template v-if="changeType == 0">
            <camera
                v-if="!isError.value"
                class="custom-camera"
                mode="scanCode"
                device-position="back"
                flash="off"
                @error="error"
                @scancode="scanCode"></camera>
            <view class="custom-camera-text">请扫描提货码</view>
            <view class="manual-type" @tap="changeOperation(1)">
                <view class="manual-type-icon">
                    <image class="custom-icon" :src="getImgOssUrl('/ui-prod-2.9.0/icon-hand.png')"></image>
                </view>
                <text>手动输入</text>
            </view>
        </template>
        <template v-if="changeType == 1">
            <view class="custom-input">
                <image class="custom-icon-search" :src="getImgOssUrl('/ui-prod-2.0.0/search_1.1.png')"></image>
                <input
                    v-model="pickCode"
                    class="custom-input-main"
                    placeholder-class="custom-input-main-placeholder"
                    placeholder="请输入提货码"
                    type="text"
                    :maxlength="40"
                    confirm-type="search"
                    @confirm="searchConfirm" />
            </view>
            <view class="manual-type" @tap="changeOperation(0)">
                <view class="manual-type-icon">
                    <image class="custom-icon" :src="getImgOssUrl('/ui-prod-2.9.0/icon-code.png')"></image>
                </view>
                <text>扫一扫</text>
            </view>
        </template>
        <!-- todo:隐私监听 -->
        <!-- <el-privacy-listener-popup></el-privacy-listener-popup> -->
        <!-- <el-privacy-popup></el-privacy-popup> -->
    </view>
</template>
<script setup>
import { ref } from 'vue';
import router from '@/router';
import request from '@/request';
import { debounce, throttle } from 'lodash';
import { getImgOssUrl } from '@/commons/utils';
import { WITH_CTX } from '@vue/compiler-dom';
import { onShow } from '@dcloudio/uni-app';
import { unaryExpression } from '@babel/types';

const changeType = ref(0);
let pickCode = ref();
let isScanSuccess = ref(false);

let isError = ref(false);
const error = error => {
    // uni.showToast({
    //     title: '您已禁止了摄像头权限，无发使用扫描二维码',
    //     icon: 'none',
    //     duration: 2000,
    // });
    uni.showModal({
        title: '您已禁止了摄像头权限，无法使用扫描二维码，请前往权限页面设置',
        icon: 'none',
        showCancel: false,
        success: () => {
            isError.value = true;
            uni.openSetting({});
        }
    });
};
const scanCode = res => {
    if (isScanSuccess.value) {
        // 防止频繁请求
        return;
    }
    // 是否需要判断QRCODE,res?.detail?.type === 'QR_CODE'
    // ios:QR_CODE Android:qrcode
    if (res?.detail?.result && /qr.?code/i.test(res?.detail?.type)) {
        // 提货码去获取订单信息
        isScanSuccess.value = true; // 防止频繁请求
        jumpToOrder(res?.detail?.result);
    }
};
const searchConfirm = async () => {
    if (!pickCode.value) {
        uni.showToast({
            title: '请输入提货码',
            icon: 'none',
            duration: 2000
        });
        return;
    }
    jumpToOrder(pickCode.value);
};
const jumpToOrder = debounce(async code => {
    try {
        uni.showLoading({
            title: '识别提货码中...',
            mask: true
        });
        const { data } = await request('staffCenterPickupOrder', { code });
        uni.hideLoading();
        setTimeout(() => {
            // 防止频繁请求
            isScanSuccess.value = false;
        }, 2000);
        router.push({
            path: '/pages-user/orderdetail/index',
            query: {
                orderId: data?.orderId, //订单ID
                type: 1
            }
        });
    } catch (error) {
        uni.hideLoading();
        uni.showToast({
            title: error?.data?.msg || '服务异常',
            icon: 'none',
            duration: 2000
        });
        setTimeout(() => {
            // 防止频繁请求
            isScanSuccess.value = false;
        }, 2000);
    }
}, 100);

const changeOperation = val => {
    pickCode.value = null;
    changeType.value = val;
};

onShow(() => {
    if (isError.value) {
        isError.value = false;
        uni.redirectTo({
            url: '/pages-user/helper/verification/index'
        });
    }
});
</script>
<style lang="scss" scoped src="./index.scss"></style>
